/**
 * 
 */
package com.scpii.universal.hibernate.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.dao.AppPageViewDao;
import com.scpii.universal.hibernate.entity.AppPageViewTab;


/**
 * universalWeb
 * @author liuhj
 * 2012-7-23下午10:01:51
 */
@Repository
public class AppPageViewDaoImpl extends BaseDaoImpl<AppPageViewTab, Integer> implements AppPageViewDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<AppPageViewTab> getViewsInfoByPageId(int pageId,
			String[] properties, boolean orderBy) {
		StringBuffer hql = new StringBuffer();
		hql.append(generateHqlByProperties(properties));
		hql.append("where entity.intDeleteSign=? and entity.intResourceId=? ");
		if (orderBy) {
			hql.append("order by entity.intViewOrder asc");
		}
		return (List<AppPageViewTab>) getHibernateTemplate()
				.getSessionFactory().getCurrentSession().createQuery(hql.toString())
				.setParameter(0, GenericConstants.DELETE_SIGN_EXIST)
				.setParameter(1, pageId).list();
	}

	@Override
	public void delete(Integer id) {
		String hql = "update AppPageViewTab a set a.intDeleteSign=?,a.datUpdateDatetime=? "
				+ "where a.intViewId=?";
		getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql).setParameter(0,
						GenericConstants.DELETE_SIGN_DELETED).setParameter(
						1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.format(new Date())).setParameter(2, id).executeUpdate();
	}

	@Override
	public AppPageViewTab getViewInfoById(int viewId, String[] properties) {
		String hql = generateHqlByProperties(properties) + "where entity.intViewId=?";
		return (AppPageViewTab) getHibernateTemplate()
				.getSessionFactory().getCurrentSession().createQuery(hql)
				.setParameter(0, viewId).uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AppPageViewTab> getStyleByCateIdFromCateR(int cateId, int type) {
		String hql = "select new AppPageViewTab(a.intViewId,a.intModuleStyleId) from AppPageViewTab a"
				+ ",AppViewCatelogRalationTab r where ";
		if (type == 1) {
			hql += "a.intResourceId=r.intResourceId ";
		} else {
			hql += "a.intViewId=r.intViewId ";
		}
		hql += "and r.intDeleteSign=? and r.intCateId=?";
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql)
				.setParameter(0, GenericConstants.DELETE_SIGN_EXIST)
				.setParameter(1, cateId).list();
	}

	@Override
	public List<AppPageViewTab> getStyleByCateIdFromContR(int cateId) {
		String hql = "select new AppPageViewTab(a.intViewId,a.intModuleStyleId) from AppPageViewTab a"
				+ ",AppViewContentRalationTab r where a.intViewId=r.intViewId and a.intDeleteSign=? and r.intDeleteSign=? and r.intBindType=? and r.intContentId=?";
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql)
				.setParameter(0, GenericConstants.DELETE_SIGN_EXIST)
				.setParameter(1, GenericConstants.DELETE_SIGN_EXIST)
				.setParameter(2, GenericConstants.ISBINDLISTDIRECTLY_YES)
				.setParameter(3, cateId).list();
	}
}
